package com.vcredit.creditcat.service.catfood;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.vcredit.creditcat.dto.catfood.CustCatfoodLogDto;
import com.vcredit.creditcat.dto.catfood.PrivilegeCatDto;
import com.vcredit.creditcat.model.CatfoodTypeName;
import com.vcredit.creditcat.model.Cust;
import com.vcredit.creditcat.model.CustCatfood;
import com.vcredit.creditcat.model.CustCatfoodLog;
import com.vcredit.creditcat.model.PrivilegeCat;
import com.vcredit.creditcat.service.CatfoodTypeNameService;
import com.vcredit.creditcat.service.CustCatfoodLogService;
import com.vcredit.creditcat.service.CustCatfoodService;
import com.vcredit.creditcat.service.PrivilegeCatService;
import com.vcredit.creditcat.util.Utility;
import com.vcredit.framework.annotation.Log;
import com.vcredit.framework.exception.BaseException;

@Service
public class CatfoodService {

	@Autowired
	private CustCatfoodService custCatfoodService;
	@Autowired
	private CatfoodTypeNameService catfoodTypeNameService;
	@Autowired
	private CustCatfoodLogService custCatfoodLogService;
	@Autowired
	private PrivilegeCatService privilegeCatService;
	
	/**
	 * 查询用户剩余猫粮
	 * @author zhaoqi01
	 * @param cust
	 * @return
	 */
	@Log
	public Long queryCustRemainCatfood(Cust cust) {
		CustCatfood custCatfood = custCatfoodService.findByCustId(cust.getCustId());
		if(null == custCatfood) {
			return 0L;
		}
		return custCatfood.getRemainCatfood();
	}

	/**
	 * 查询用户猫粮状态
	 * @param cust
	 * @return
	 */
	@Log
	public CustCatfood queryCustCatfood(Cust cust) {
		CustCatfood custCatfood = custCatfoodService.findByCustId(cust.getCustId());
		return custCatfood;
	}

	/**
	 * 用户猫粮变更
	 * @author zhaoqi01
	 * @param catfood
	 * @param typeId
	 * @param cust
	 * @throws BaseException 
	 */
	@Log
	@Transactional
	public void updateCustCatfood(Long catfood, Long typeId, Cust cust) throws BaseException {
		Map<Long, CatfoodTypeName> findForMap = catfoodTypeNameService.findForMap();
		CatfoodTypeName catfoodTypeName = findForMap.get(typeId.longValue());
		if(null == catfoodTypeName) {
			throw new BaseException("参数错误");
		}
		CustCatfood custCatfood = custCatfoodService.findByCustId(cust.getCustId());
		long absCatfood = Math.abs(catfood); // 取绝对值
		if(null == custCatfood) { // 新增用户猫粮统计
			custCatfood = new CustCatfood();
			custCatfood.setCustId(cust.getCustId());
			custCatfood.setRemainCatfood(absCatfood);
			custCatfood.setTotalCatfood(custCatfood.getRemainCatfood());
			custCatfoodService.insert(custCatfood);
		} else { // 更新用户猫粮统计
			if(catfoodTypeName.getType() == 1) { // 增加猫粮
				custCatfood.setRemainCatfood(absCatfood + custCatfood.getRemainCatfood());
				custCatfood.setTotalCatfood(absCatfood + custCatfood.getTotalCatfood());
			} else { // 减少猫粮
				// 健壮性判断用户剩余猫粮需要大等于现在需要扣除的猫粮数
				if(custCatfood.getRemainCatfood() < absCatfood) {
					throw new BaseException("猫粮不足");
				}
				custCatfood.setRemainCatfood(custCatfood.getRemainCatfood() - absCatfood);
			}
			custCatfoodService.update(custCatfood);
		}
		
		// 新增用户猫粮进出日志
		CustCatfoodLog custCatfoodLog = new CustCatfoodLog();
		custCatfoodLog.setCustId(cust.getCustId());
		custCatfoodLog.setCatfoodTypeId(catfoodTypeName.getId());
		if(catfoodTypeName.getType() == 1) { // 增加猫粮
			custCatfoodLog.setCatfood(absCatfood);
		} else { // 减少猫粮
			custCatfoodLog.setCatfood(0 - absCatfood);
		}
		custCatfoodLogService.insert(custCatfoodLog);
	}

	/**
	 * 查询用户猫粮记录
	 * @author zhaoqi01
	 * @param cust
	 * @return
	 */
	@Log
	public List<CustCatfoodLogDto> queryCustCatfoodLog(Cust cust) {
		List<CustCatfoodLogDto> result = new ArrayList<CustCatfoodLogDto>();
		List<CustCatfoodLog> list = custCatfoodLogService.findByCustIdOrderBy(cust.getCustId());
		if(Utility.isNullOrEmpty(list)) {
			return result;
		}
		Map<Long, CatfoodTypeName> findForMap = catfoodTypeNameService.findForMap();
		for (CustCatfoodLog custCatfoodLog : list) {
			CustCatfoodLogDto dto = new CustCatfoodLogDto(custCatfoodLog.getCatfood(), 
					findForMap.get(custCatfoodLog.getCatfoodTypeId()).getDesc(), 
					custCatfoodLog.getCreatedTime());
			result.add(dto);
		}
		return result;
	}
	
	
	
	
	
	
	/**
	 * 判断今天是否可以签到
	 * @author zhaoqi01
	 * @param cust
	 * @return
	 */
	@Log
	public Boolean canSignInToday(Cust cust) {
		// TODO 猫粮类型id待确定
		Long count = custCatfoodLogService.findByParamToday(cust.getCustId(),1L);
		if(count == 0) {
			return true;
		}
		return false;
	}
	
	/**
	 * 查询用户特权猫列表
	 * @author zhaoqi01
	 * @param cust
	 * @return
	 */
	@Log
	public List<PrivilegeCatDto> queryCustPrivilegeCatList(Cust cust) {
		List<PrivilegeCatDto> result = new ArrayList<PrivilegeCatDto>();
		Map<Integer, PrivilegeCat> findAllForMap = privilegeCatService.findAllForMap();
		//Long custTotalCatfood = 0L;
//		CustCatfood custCatfood = custCatfoodService.findByCustId(cust.getCustId());
//		if(custCatfood != null) {
//			custTotalCatfood = custCatfood.getTotalCatfood();
//		}
		// TODO，查询用户当前的喵卡，根据喵卡查询可以获取的特权猫
		for(PrivilegeCat privilegeCat : findAllForMap.values()) {
			if(!privilegeCat.getStatus()) { // 如果该特权猫下架，前端不显示
				continue;
			}
			PrivilegeCatDto dto = new PrivilegeCatDto();
			dto.setName(privilegeCat.getName());
			dto.setTitle(privilegeCat.getTitle());
			privilegeCat.setJumpUrl(privilegeCat.getJumpUrl());
//			if(custTotalCatfood < privilegeCat.getMinCatfood().longValue()) {
//				// 用户总共获取的猫粮如果少于该特权猫所需要的猫粮，则置灰该特权猫
//				dto.setOpenStatus(false);
//				dto.setBtnText(privilegeCat.getBtnTextDef());
//				dto.setIcon(privilegeCat.getCloseIcon());
//			} else {
//				dto.setOpenStatus(true);
//				dto.setBtnText(privilegeCat.getBtnText());
//				dto.setIcon(privilegeCat.getOpenIcon());
//			}
			result.add(dto);
		}
		return result;
	}
	
	
	/**
	 * 根据类型查询猫粮类型的默认值
	 * @author zhaoqi01
	 * @return
	 */
	@Log
	public CatfoodTypeName queryCustCatfoodTypeName(long id) {		
	     return catfoodTypeNameService.findById(id);
	}
}
